package typejump;

/**
 * This is a Singleton-style class which keeps track of the Swag for the
 * entire application.
 * @author g
 *
 */
public class Swagger {

	private int maxswag = 100;
	private double swag = maxswag;
	private boolean infinite = false;
	
	private static Swagger me;
	private Swagger() {}
	public static Swagger getInst() {
		if (me == null) me = new Swagger();
		return me;
	}
	
	public synchronized boolean hasInfiniteSwag() {
		return infinite;
	}
	
	/**
	 * If set to true, the meter will always report that it always has enough swag.
	 * @param b
	 */
	public synchronized void setInfiniteSwag(boolean b) {
		this.infinite = b;
	}
	
	/**
	 * Returns true if the meter has at least that much swag.
	 * @param swag
	 * @return
	 */
	public boolean hasSwag(double swag) {
		return infinite || this.swag >= swag;
	}
	
	/**
	 * Expends [swag] swag, if there's enough. If not, no swag is expended.
	 * @param swag
	 * @return True if there was enough, and False if there isn't.
	 */
	public boolean tryExpendSwag(double swag) {
		if (infinite) return true;
		if (swag < 0) return false;
		if (swag > this.swag) {
			return false;
		}
		this.swag -= swag;
		return true;
	}
	
	/**
	 * Expends as much swag as it can, up to the parameter amount or the total
	 * amount of swag in the meter. Returns the amount of swag it actually expent.
	 * @param swag
	 * @return
	 */
	public double expendSwag(double swag) {
		if (infinite) return swag;
		if (swag < 0) return 0;
		double toExpend = Math.min(swag, this.swag);
		this.swag -= toExpend;
		return toExpend;
	}
	
	/**
	 * Returns the amount of swag currently in the meter.
	 * @return
	 */
	public int getSwag() {
		return (int)swag;
	}
	
	/**
	 * Returns the maximum amount of swag we can have.
	 * @return
	 */
	public int getMaxSwag() {
		return maxswag;
	}
	
	/**
	 * Retores [swag] amount of swag to the swag-o-meter, up to the maxswag.
	 * @param swag
	 */
	public synchronized void restoreSwag(double swag) {
		this.swag = Math.min(this.swag + swag, this.maxswag);
	}
	
	/**
	 * Restores all the swag to the swag-o-meter.
	 */
	public synchronized void restoreSwag() {
		this.swag = this.maxswag;
	}
	
	/**
	 * Increases the maximum amount of swag one can have (this also restores [amount] of swag,
	 * so if you increase the maxswag when you're at full swag you stay at full).
	 * @param amount
	 */
	public synchronized void increaseMaxSwag(int amount) {
		if (amount < 0) return;
		this.maxswag += amount;
		this.swag += amount;
	}
	
	/**
	 * Sets the maximum amount of swag this swag-o-meter can hold. If
	 * this is a number higher than the original amount, the amount
	 * of swag currently in the meter will increase correspondingly.
	 * @param amount
	 */
	public synchronized void setMaxSwag(int amount) {
		int dif = amount - this.maxswag;
		this.maxswag = amount;
		
		if (dif > 0) {
			this.swag += dif;
		} else if (this.swag > this.maxswag) {
			this.swag = this.maxswag;
		}
	}
}
